"use strict";
class Food {
    constructor() {
        this.element = document.querySelector('.food');
    }
    // 获取食物X轴
    get x() {
        return this.element.offsetLeft;
    }
    // 获取食物Y轴
    get y() {
        return this.element.offsetTop;
    }
    // 随机修改食物位置
    modifyLocation() {
        let box = document.querySelector('.box'); //获取box盒子元素
        // 随机位置要求要在 10的倍数内出现。等价于Math.round(Math.random() * 屏幕所占大小-食物大小/10)*10
        this.element.style.left = Math.round(Math.random() * (box.clientWidth - 10) / 10) * 10 + 'px';
        this.element.style.top = Math.round(Math.random() * (box.clientHeight - 10) / 10) * 10 + 'px';
        //region 测试生成的随机数是否符合要求
        // let arr:number[] =[];
        // for (let i = 0; i < 1000; i++) {
        //     arr[i] = Math.round(Math.random() * (box.clientHeight-10) / 10) * 10;
        // }
        // arr.forEach((value, index) => {
        //     if (value < 0 || value > 290) {
        //         console.log(false);
        //     }else {
        //         console.log(true);
        //     }
        // })
        //endregion
    }
}
class Detail {
    constructor() {
        this.score = 0;
        this.level = 1;
        this.maxLevel = 10; //最大等级限制
        this.scoreEle = document.querySelector('.score>span');
        this.levelEle = document.querySelector('.level>span');
    }
    // 增加分数
    scoreUp() {
        this.scoreEle.innerHTML = ++this.score + '';
        if (this.score % 10 === 0)
            this.levelUp(); //每获得十分会提升一个等级
    }
    // 增加等级
    levelUp() {
        if (this.level < this.maxLevel)
            this.levelEle.innerHTML = ++this.level + ''; //小于等级限制则增加
    }
}
class Snake {
    constructor() {
        this.snakeEle = document.querySelector('.snake');
        this.head = document.querySelector('.head');
        this.bodies = this.snakeEle.querySelectorAll('.bodies');
    }
    //     获取蛇头坐标
    get x() {
        return this.head.offsetLeft;
    }
    get y() {
        return this.head.offsetTop;
    }
    set x(value) {
        this.head.style.left = value + 'px';
    }
    set y(value) {
        this.head.style.top = value + 'px';
    }
    addBody() {
        this.snakeEle.insertAdjacentHTML("beforeend", "<div></div>");
    }
}
class GameControl {
    constructor() {
        this.snake = new Snake();
        this.food = new Food();
        this.detail = new Detail();
        this.direction = 'ArrowRight';
    }
    //     游戏初始化方法
    init() {
        document.addEventListener('keydown', this.keyDownHandler.bind(this));
        this.move();
    }
    //     处理键盘响应事件 ArrowUp ArrowDown ArrowLeft ArrowRight
    keyDownHandler(e) {
        if (e.key === 'ArrowUp' || 'ArrowDown' || 'ArrowLeft' || 'ArrowRight') {
            this.direction = e.key;
            console.log(this.direction);
        }
    }
    //     移动
    move() {
        if (this.snake.x <= 290 && this.snake.y <= 290) {
            console.log(this.snake.x);
            console.log('11', this.snake.y);
            switch (this.direction) {
                case 'ArrowUp':
                    console.log('up');
                    this.snake.y -= 10;
                    break;
                case 'ArrowDown':
                    this.snake.y += 10;
                    break;
                case 'ArrowLeft':
                    this.snake.x -= 10;
                    break;
                case 'ArrowRight':
                    this.snake.x += 10;
                    break;
            }
            // 根据关卡等级，移动速度每升一级速度加快10%
            setTimeout(this.move.bind(this), 300 - (this.detail.level - 1) * 30);
        }
    }
}
//region 实例化对象
let food = new Food();
let detail = new Detail();
let snake = new Snake();
let gameControl = new GameControl();
//endregion
food.modifyLocation();
gameControl.init();
